package com.edu.zut.security;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.interfaces.Claim;
import com.edu.zut.entity.User;
import com.edu.zut.mapper.RoleMapper;
import com.edu.zut.mapper.UserMapper;
import com.edu.zut.mapper.UserRoleMapper;
import com.edu.zut.utils.JwtUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author 许文科
 * @Date 2023/1/12 19:27
 * @Description paperdetect-bcak
 */
@SpringBootTest
public class jwt {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Test
    public void testUserMapper() {
        List<User> users = userMapper.selectList(null);
        for (User item : users) {
            System.out.println(item.getUserNumber());
        }
    }

    @Test
    public void testJWT() {
        List<Long> roleids = userRoleMapper.selectRoleIdsByUserId(1L);
        List<String> roleName = roleMapper.selectRoleNameByRoleIds(roleids);
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        for (String item : roleName) {
            authorities.add(new SimpleGrantedAuthority(item));
        }
        String token = JwtUtil.sign("xuwenke", roleName);

        System.out.println(JSON.toJSONString(roleName));
        System.out.println(JSON.toJSONString(authorities));

        List<String> r = new ArrayList<>();
        for (GrantedAuthority item : authorities) {
            r.add(item.getAuthority());
        }
        System.out.println("r：" + JSON.toJSONString(r));


        System.out.println(token);
        boolean flag = JwtUtil.verify(token);
        System.out.println(flag);
        Map<String, Claim> claimMap = JwtUtil.getClaimsFormToken(token);
        System.out.println(claimMap.get("roles").asString());
        System.out.println(claimMap.get("userName").asString());

    }

    @Test
    public void show1() {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6IltcIlJPTEVfc3R1ZGVudFwiLFwiUk9MRV90ZWFjaGVyXCJdIiwiaXNzIjoiYXV0aDAiLCJ1c2VyTmFtZSI6Inh1d2Vua2UiLCJleHAiOjE2NzM2Nzk0MjN9.HmSAblU1ocLDq7eCj9w8pCILTAYazzfSS7otZTocpc0";
        boolean t = JwtUtil.verify(token);
        System.out.println(t);
    }

    @Test
    public void show2() {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCIsInVzZXJOYW1lIjoieHV3ZW5rZSIsImV4cCI6MTY3MzczMDY3NCwicm9sZU5hbWVzIjoiW1wiUk9MRV90ZWFjaGVyXCIsXCJST0xFX3N0dWRlbnRcIl0ifQ.kVTDWyrt2KIyvdtqdQ6HRzRyAa5219yK-VDbVd6CBtk";

        Map<String, Claim> claimsFormToken = JwtUtil.getClaimsFormToken(token);
        System.out.println(claimsFormToken.get("userName").asString());
        List<String> roleNames = JSON.parseArray(claimsFormToken.get("roleNames").asString(), String.class);
        System.out.println(roleNames);
    }
}
